home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 March / Macworld (1998-03) (Disk 1).dmg / Shareware World / Utilities / Text Processing / Alpha / Tcl / Packages / recentFiles.tcl < prev    next >
Encoding:
Text File  |  1997-12-13  |  3.6 KB  |  116 lines  |  [TEXT/ALFA]

  1. ## -*-Tcl-*- (install)
  2.  # ###################################################################
  3.  #  Alpha - new Tcl folder configuration
  4.  # 
  5.  #  FILE: "recentFiles.tcl"
  6.  #                                    created: 21/9/97 {9:14:38 pm} 
  7.  #                                last update: 13/12/97 {12:36:24 am} 
  8.  #  
  9.  # Reorganisation carried out by Vince Darley with much help from Tom 
  10.  # Fetherston, Johan Linde and suggestions from the Alpha-D mailing list.  
  11.  # Alpha is shareware; please register with the author using the register 
  12.  # button in the about box.
  13.  #  
  14.  # original author probably Pete Keleher.  Vince added a bunch of
  15.  # code to work-around some Alpha-menu problems, and made it a 
  16.  # package.
  17.  # 
  18.  # Version 0.2 builds the menu about a zillion times faster than
  19.  # the original which built the menu once for every item at
  20.  # startup!  Also removed two unnecessary procs, since that stuff
  21.  # can be done elsewhere automatically.
  22.  # ###################################################################
  23.  ##
  24.  
  25. alpha::extension recentFilesMenu 0.2.3 {
  26.     namespace eval recent {}
  27.     ensureset recent::Files ""
  28.     hook::register saveasHook recent::push
  29.     hook::register openHook recent::push
  30.     menu::buildProc recent recent::makeMenu
  31.     menu::insert File submenu 2 recent
  32.     lappend modifiedVars recent::Files
  33. } help {Adds a menu of recent files to the files menu}
  34.  
  35. newPref variable numberOfRecentFiles 15
  36. ## 
  37.  # -------------------------------------------------------------------------
  38.  # 
  39.  # "recent::push" --
  40.  # 
  41.  #  Works with files whose name contained '[' or ']' which didn't before.
  42.  #  Doesn't add any file which fails 'file exists' to the menu.
  43.  #  I also increased the number of files to 15.  There should really
  44.  #  be a global flag to adjust for that.
  45.  # -------------------------------------------------------------------------
  46.  ##
  47. proc recent::push {name {name2 ""}} {
  48.     if {$name2 != ""} { set name $name2 }
  49.     global recent::Files numberOfRecentFiles
  50.     
  51.     regsub { <\w+>$} $name {} name
  52.     if ![file exists $name] { return }
  53.     regsub -all {\\([][])} $name {\1} name
  54.     if {[info exists recent::Files] \
  55.       && ([set ind [lsearch -regexp ${recent::Files} ":[quote::Regfind [file tail $name]]…?$"]] >= 0)} {
  56.         set recent::Files [lreplace ${recent::Files} $ind $ind]
  57.         lappend recent::Files $name
  58.         return
  59.     }
  60.     lappend recent::Files $name
  61.     if {[llength ${recent::Files}] > $numberOfRecentFiles} {
  62.         set recent::Files [lrange ${recent::Files} 1 end]
  63.     }
  64.     recent::makeMenu
  65. }
  66.  
  67. proc recent::makeMenu {} {
  68.     global recent::Files
  69.     set tails [map {file tail} ${recent::Files}]
  70.     menu -m -c -n recent -p recent::menuProc \
  71.       [concat [lsort -ignore $tails] [list "(-" "Reset List"]]
  72.     set enable [expr [llength ${recent::Files}] ? 1 : 0]
  73.     enableMenuItem File recent $enable
  74.  
  75. }
  76.  
  77. ## 
  78.  # -------------------------------------------------------------------------
  79.  # 
  80.  # "recent::menuProc" --
  81.  # 
  82.  #  Works with menu items which contain '[', ']' and '…' which didn't work
  83.  #  before.
  84.  # -------------------------------------------------------------------------
  85.  ##
  86. proc recent::menuProc {menu name} {
  87.     global recent::Files
  88.     
  89.     if {$name == "Reset List"} {
  90.         set recent::Files {}
  91.         menu -m -n recent -p recent::menuProc {}
  92.         recent::makeMenu
  93.     } else {
  94.         if {[set ind [lsearch -regexp ${recent::Files} ":[quote::Regfind $name]…?$"]] >= 0} {
  95.             edit [lindex ${recent::Files} $ind]
  96.         } else {
  97.             alpha::errorAlert "Couldn't find a file '$name'.  Weird!"
  98.         }
  99.     }
  100. }
  101.  
  102. proc recent::editFile {} {
  103.     global recent::Files
  104.     
  105.     foreach f ${recent::Files} {
  106.         lappend tails [file tail $f]
  107.     }
  108.     
  109.     foreach res [listpick -l -p {File?} [lsort -ignore $tails]]  {
  110.         set ind [lsearch ${recent::Files} \*:$res]
  111.         catch {edit [lindex ${recent::Files} $ind]}
  112.     }
  113. }
  114.  
  115.  
  116.